//
//  CommonQuestionsViewController.m
//  PigMarket
//
//  Created by mac on 2019/4/2.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//


#pragma mark --------- 常见问题

#import "CommonQuestionsViewController.h"
#import "commonQuestionsCell.h"
#import "CommonQuestionsHeaderView.h"
#import "QuestionBottomView.h"
#import "QuestionViewController.h"

@interface CommonQuestionsViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 tableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
/**
 headerView
 */
@property (nonatomic , strong) CommonQuestionsHeaderView *headerView;
/**
 bottomView
 */
@property (nonatomic , strong) QuestionBottomView *bottomView;

@end

@implementation CommonQuestionsViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    self.title = @"常见问题";
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
    [self.view addSubview:self.bottomView];
}

- (void) initLayout {
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-20);
    }];
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_equalTo(0);
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.bottomView.mas_top);
    }];
}

- (void) initAction {
     @weakify(self);
    [self.headerView.questionTypeBtnArray enumerateObjectsUsingBlock:^(HButtonView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        @strongify(self);
        obj.tapped = ^{
            [self questionsTappedWithIdx:idx];
        };
    }];
    
}

- (void)initData {
    
    [[[PigMarketApi sharedInstance] getCommonQuestionsWithQuestionType:@9] subscribeNext:^(CommonQuestionsList * _Nullable commonQuestionsList) {
        [self.dataArr addObjectsFromArray:commonQuestionsList.questions];
        [self.myTableView reloadData];
    } error:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommonQuestionsCell *cell = [CommonQuestionsCell cellWithTableView:tableView];
    if (indexPath.row == 0) {
        cell.isHiddenTopView = NO;
    } else {
        cell.isHiddenTopView = YES;
    }
    if (self.dataArr.count > 0 && self.dataArr.count > indexPath.row) {
        CommonQuestions *questionModel = self.dataArr[indexPath.row];
        cell.questionsModel = questionModel;
    }
   
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
#pragma mark - target action
- (void)questionsTappedWithIdx:(NSUInteger)idx {
    //匹配请求的入参type
    NSUInteger currentType = idx + 1;
    QuestionViewController *detailVC = [[QuestionViewController alloc] init];
    detailVC.type = currentType;
    [self.navigationController pushViewController:detailVC animated:YES];
}
#pragma mark - private method


#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.estimatedRowHeight = 100;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.showsVerticalScrollIndicator = NO;
        _myTableView.tableHeaderView = self.headerView;
        _myTableView.backgroundColor = [UIColor globalBackgroundColor];
    }
    return _myTableView;
}
- (CommonQuestionsHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[CommonQuestionsHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160)];
    }
    return _headerView;
}
- (QuestionBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[QuestionBottomView alloc] init];
    }
    return _bottomView;
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
@end
